package com.foodorder.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 配送跟踪实体类
 */
@Data
@Entity
@Table(name = "delivery_tracking")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeliveryTracking {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 订单ID
     */
    @Column(nullable = false, unique = true)
    private Long orderId;

    /**
     * 配送员ID
     */
    private Long deliveryStaffId;

    /**
     * 配送状态
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private DeliveryStatus status = DeliveryStatus.PENDING;

    /**
     * 取餐位置纬度
     */
    @Column(nullable = false, precision = 10, scale = 6)
    private BigDecimal pickupLatitude;

    /**
     * 取餐位置经度
     */
    @Column(nullable = false, precision = 10, scale = 6)
    private BigDecimal pickupLongitude;

    /**
     * 配送位置纬度
     */
    @Column(nullable = false, precision = 10, scale = 6)
    private BigDecimal deliveryLatitude;

    /**
     * 配送位置经度
     */
    @Column(nullable = false, precision = 10, scale = 6)
    private BigDecimal deliveryLongitude;

    /**
     * 当前位置纬度
     */
    @Column(precision = 10, scale = 6)
    private BigDecimal currentLatitude;

    /**
     * 当前位置经度
     */
    @Column(precision = 10, scale = 6)
    private BigDecimal currentLongitude;

    /**
     * 配送距离（米）
     */
    private Integer deliveryDistance;

    /**
     * 预计送达时间
     */
    private LocalDateTime estimatedDeliveryTime;

    /**
     * 实际送达时间
     */
    private LocalDateTime actualDeliveryTime;

    /**
     * 接单时间
     */
    private LocalDateTime acceptedTime;

    /**
     * 取餐时间
     */
    private LocalDateTime pickupTime;

    /**
     * 开始配送时间
     */
    private LocalDateTime startDeliveryTime;

    /**
     * 配送轨迹（JSON格式存储经纬度列表）
     */
    @Column(columnDefinition = "TEXT")
    private String deliveryRoute;

    /**
     * 备注
     */
    @Column(length = 500)
    private String remark;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    /**
     * 配送状态枚举
     */
    public enum DeliveryStatus {
        /**
         * 待分配配送员
         */
        PENDING,
        
        /**
         * 已分配配送员
         */
        ASSIGNED,
        
        /**
         * 配送员已接单
         */
        ACCEPTED,
        
        /**
         * 配送员到店
         */
        ARRIVED_AT_STORE,
        
        /**
         * 配送员已取餐
         */
        PICKED_UP,
        
        /**
         * 配送中
         */
        IN_DELIVERY,
        
        /**
         * 已送达
         */
        DELIVERED,
        
        /**
         * 配送失败
         */
        FAILED,
        
        /**
         * 已取消
         */
        CANCELLED
    }
} 